<?php

namespace app\admin\controller;
use app\admin\model\ApiType;
use app\api\model\ApiParam;

class ApiTypeController extends BaseController
{
    public function index()
    {
        $param =  new ApiParam();
        $param = $param->select();
        $this->assign('param',$param);
        return $this->fetch();
    }
    /**
     * author：廖桂钦
     * eamil : artistCode@163.com
     * create time :2020/4/8
     * description[读取api 列表]
     */
    public function read(){
       $Api =  new ApiType();
       $count   = $Api->count();

       $data = $Api->page(input('get.page'),input('get.limit'))->select();
        return json(array('code' => 0, 'msg' => "", 'data' => $data, 'count' =>$count ));
    }

    public function add()
    {
        $Api_type = (new ApiType());
        $msg = '添加成功';
        $data = input('post.');
        $data['api_param_id'] = join($data['api_param_id'],',');
        $status = $Api_type->save($data);
        $code = 0;
        if (!$status) {
            $msg = '添加失败';
            $code = 500;
        }
        return ['msg' => $msg, 'code' => $code];
    }
    public function upd(){
        $Api_type = new ApiType();
        $data = input('post.');
        $data['api_param_id'] = join($data['api_param_id'],',');
        $status = $Api_type->isUpdate(true)->save($data);
        $msg = '添加成功';
        $code = 0;
        if (!$status){
            $msg = '添加失败';
            $code = '500';
        }
        return ['msg'=>$msg,'code'=>$code];
    }
    public function del(){
        $status = ApiType::where(input('post.'))->delete();
        if ($status){
            return ['msg'=>'删除成功','code'=>0];
        }else{
            return ['msg'=>'删除失败','code'=>500];
        }
    }

}
